package com.galaxy.order.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.galaxy.common.core.domain.R;
import com.galaxy.common.core.validate.AddGroup;
import com.galaxy.common.core.validate.EditGroup;
import com.galaxy.common.log.annotation.Log;
import com.galaxy.common.log.enums.BusinessType;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.order.domain.Order;
import com.galaxy.order.domain.bo.OrderBo;
import com.galaxy.order.domain.vo.OrderVo;
import com.galaxy.order.service.OrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 订单信息Controller
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 查询订单信息列表
     */
    @GetMapping("/list")
    @SaCheckPermission("order:order:list")
    public TableDataInfo<OrderVo> list(OrderBo bo, PageQuery pageQuery) {
        return orderService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping("/{id}")
    @SaCheckPermission("order:order:list")
    public R<OrderVo> getInfo(@PathVariable("id") Long id) {
        return R.ok(orderService.queryById(id));
    }

    /**
     * 新增订单信息
     */
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    @SaCheckPermission("order:order:add")
    public R<Order> add(@Validated(AddGroup.class) @RequestBody OrderBo bo) {
        return R.ok(orderService.insertByBo(bo));
    }

    /**
     * 修改订单信息
     */
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @SaCheckPermission("order:order:edit")
    public R<Boolean> edit(@Validated(EditGroup.class) @RequestBody OrderBo bo) {
        return R.ok(orderService.updateByBo(bo));
    }

    /**
     * 删除订单信息
     */
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @SaCheckPermission("order:order:remove")
    public R<Boolean> remove(@PathVariable Long[] ids) {
        return R.ok(orderService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 查询订单信息列表（不分页）
     */
    @GetMapping("/listOrder")
    @SaCheckPermission("order:order:list")
    public R<List<OrderVo>> list(OrderBo bo) {
        return R.ok(orderService.queryList(bo));
    }

    /**
     * 获取今日订单数
     */
    @GetMapping("/today/count")
    public R<Long> getTodayOrderCount() {
        return R.ok(orderService.getTodayOrderCount());
    }

    /**
     * 获取今日销售额
     */
    @GetMapping("/today/sales")
    public R<BigDecimal> getTodaySales() {
        return R.ok(orderService.getTodaySales());
    }
}
